package com.ibeeking.found.open.b.rest.service.mq;

import com.ibeeking.found.common.constants.WxCpServiceTypeConstant;
import com.ibeeking.found.common.entity.MqMsgEventParam;
import com.ibeeking.found.common.entity.open.OpenAuthDTO;
import com.ibeeking.found.common.entity.open.OpenAuthQuery;
import com.ibeeking.found.common.enums.OpenApiConfigEnum;
import com.ibeeking.found.common.enums.WxCpAppTypeEnum;
import com.ibeeking.found.cop.api.common.param.MsgEventDepartmentParam;
import com.ibeeking.found.cop.api.common.param.MsgEventFriendsParam;
import com.ibeeking.gmc.global.api.feign.GlobalOpenFeignClient;
import com.ibeeking.found.open.b.rest.service.IMqService;
import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.log.utils.LogUtils;
import com.ibeeking.nematos.utils.data.StringUtils;
import com.ibeeking.nematos.utils.json.JsonUtils;
import com.ibeeking.nematos.utils.result.ResponseResult;
import me.chanjar.weixin.cp.bean.message.WxCpXmlMessage;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class WxCpFriendsMsgEventHandler {

    @Resource
    private IMqService mqServiceImpl;

    @Resource
    private GlobalOpenFeignClient globalOpenFeignClient;

    //配置了客户联系功能的成员添加、修改、删除客户标签时，回调该事件
    public void processFriendsTagsMsgEvent(WxCpXmlMessage wxCpXmlMessage){
        String suiteId = wxCpXmlMessage.getAgentId();
        String authCorpId = "";

        OpenAuthDTO wxOpenAuthDTO = queryWxOpenAuthListByComponentIdAndAppId();
        if (null == wxOpenAuthDTO) {
            throw new BusinessException("无法获取开发平台参数信息");
        }
        MqMsgEventParam msgEventParam = new MqMsgEventParam();
        msgEventParam.setFromUserName(wxCpXmlMessage.getFromUserName());
        msgEventParam.setToUserName(wxCpXmlMessage.getToUserName());
        msgEventParam.setEvent(wxCpXmlMessage.getEvent());
        msgEventParam.setTenantId(wxOpenAuthDTO.getTenantId().toString());
        msgEventParam.setMsgType(wxCpXmlMessage.getMsgType());
        msgEventParam.setChangeType(wxCpXmlMessage.getChangeType());
        msgEventParam.setCreateTime(wxCpXmlMessage.getCreateTime());
        MsgEventFriendsParam friendsParam = new MsgEventFriendsParam();
        friendsParam.setId(wxCpXmlMessage.getId());
        friendsParam.setTenantId(wxOpenAuthDTO.getTenantId().toString());
        friendsParam.setTagType(wxCpXmlMessage.getTagType());
        friendsParam.setStrategyId(wxCpXmlMessage.getStrategyId());
        msgEventParam.setParams(JsonUtils.toJsonStr(friendsParam));
        LogUtils.info(">>>>>>> FriendsTags param:{}", JsonUtils.toJsonStr(msgEventParam));
        mqServiceImpl.sendMsgEventMessage(msgEventParam);
    }
    /*当企业管理员在终端/管理端调整标签顺序时，可能导致标签顺序整体调整重排，引起大部分标签的order值发生变化，
    *此时会回调此事件，收到此事件后企业应尽快全量同步标签的order值，防止后续调用接口排序出现非预期结果
    */
    public void processFriendsShuffleTagsMsgEvent(WxCpXmlMessage wxCpXmlMessage){
        String suiteId = wxCpXmlMessage.getAgentId();
        String authCorpId = "";

        OpenAuthDTO wxOpenAuthDTO = queryWxOpenAuthListByComponentIdAndAppId();
        if (null == wxOpenAuthDTO) {
            throw new BusinessException("无法获取开发平台参数信息");
        }
        MqMsgEventParam msgEventParam = new MqMsgEventParam();
        msgEventParam.setFromUserName(wxCpXmlMessage.getFromUserName());
        msgEventParam.setToUserName(wxCpXmlMessage.getToUserName());
        msgEventParam.setEvent(wxCpXmlMessage.getEvent());
        msgEventParam.setTenantId(wxOpenAuthDTO.getTenantId().toString());
        msgEventParam.setMsgType(wxCpXmlMessage.getMsgType());
        msgEventParam.setChangeType(wxCpXmlMessage.getChangeType());
        msgEventParam.setCreateTime(wxCpXmlMessage.getCreateTime());
        MsgEventFriendsParam friendsParam = new MsgEventFriendsParam();
        friendsParam.setId(wxCpXmlMessage.getId());
        friendsParam.setTenantId(wxOpenAuthDTO.getTenantId().toString());
        friendsParam.setStrategyId(wxCpXmlMessage.getStrategyId());
        msgEventParam.setParams(JsonUtils.toJsonStr(friendsParam));
        LogUtils.info(">>>>>>> FriendsTags param:{}", JsonUtils.toJsonStr(msgEventParam));
        mqServiceImpl.sendMsgEventMessage(msgEventParam);
    }
    //配置了客户联系功能的成员添加、修改、删除外部联系人时，回调该事件
    public void processFriendsMsgEvent(WxCpXmlMessage wxCpXmlMessage){
        String suiteId = wxCpXmlMessage.getAgentId();
        String authCorpId = "";

        OpenAuthDTO wxOpenAuthDTO = queryWxOpenAuthListByComponentIdAndAppId();
        if (null == wxOpenAuthDTO) {
            throw new BusinessException("无法获取开发平台参数信息");
        }
        MqMsgEventParam msgEventParam = new MqMsgEventParam();
        msgEventParam.setFromUserName(wxCpXmlMessage.getFromUserName());
        msgEventParam.setToUserName(wxCpXmlMessage.getToUserName());
        msgEventParam.setEvent(wxCpXmlMessage.getEvent());
        msgEventParam.setTenantId(wxOpenAuthDTO.getTenantId().toString());
        msgEventParam.setMsgType(wxCpXmlMessage.getMsgType());
        msgEventParam.setChangeType(wxCpXmlMessage.getChangeType());
        msgEventParam.setCreateTime(wxCpXmlMessage.getCreateTime());
        MsgEventFriendsParam friendsParam = new MsgEventFriendsParam();
        friendsParam.setTenantId(wxOpenAuthDTO.getTenantId().toString());
        friendsParam.setEmployeeCode(wxCpXmlMessage.getUserId());
        friendsParam.setFriendsCode(wxCpXmlMessage.getExternalUserId());
        friendsParam.setWelcomeCode(wxCpXmlMessage.getWelcomeCode());
        friendsParam.setState(wxCpXmlMessage.getState());
        friendsParam.setSource(wxCpXmlMessage.getSource());
        msgEventParam.setParams(JsonUtils.toJsonStr(friendsParam));
        LogUtils.info(">>>>>>> Friends Param:{}", JsonUtils.toJsonStr(msgEventParam));
        mqServiceImpl.sendMsgEventMessage(msgEventParam);
    }
    //外部联系人添加了配置了客户联系功能且开启了免验证的成员时（此时成员尚未确认添加对方为好友），回调该事件
    public void processFriendsHalfMsgEvent(WxCpXmlMessage wxCpXmlMessage){
        String suiteId = wxCpXmlMessage.getAgentId();
        String authCorpId = "";

        OpenAuthDTO wxOpenAuthDTO = queryWxOpenAuthListByComponentIdAndAppId();
        if (null == wxOpenAuthDTO) {
            throw new BusinessException("无法获取开发平台参数信息");
        }
        MqMsgEventParam msgEventParam = new MqMsgEventParam();
        msgEventParam.setFromUserName(wxCpXmlMessage.getFromUserName());
        msgEventParam.setToUserName(wxCpXmlMessage.getToUserName());
        msgEventParam.setEvent(wxCpXmlMessage.getEvent());
        msgEventParam.setTenantId(wxOpenAuthDTO.getTenantId().toString());
        msgEventParam.setMsgType(wxCpXmlMessage.getMsgType());
        msgEventParam.setChangeType(wxCpXmlMessage.getChangeType());
        msgEventParam.setCreateTime(wxCpXmlMessage.getCreateTime());
        MsgEventFriendsParam friendsParam = new MsgEventFriendsParam();
        friendsParam.setTenantId(wxOpenAuthDTO.getTenantId().toString());
        friendsParam.setEmployeeCode(wxCpXmlMessage.getUserId());
        friendsParam.setFriendsCode(wxCpXmlMessage.getExternalUserId());
        friendsParam.setWelcomeCode(wxCpXmlMessage.getWelcomeCode());
        friendsParam.setState(wxCpXmlMessage.getState());
        msgEventParam.setParams(JsonUtils.toJsonStr(friendsParam));
        LogUtils.info(">>>>>>> FriendsHalf Param:{}", JsonUtils.toJsonStr(msgEventParam));
        mqServiceImpl.sendMsgEventMessage(msgEventParam);
    }
    //配置了客户联系功能的成员被外部联系人删除时，回调该事件
    public void processDeleteFriendsFollowMsgEvent(WxCpXmlMessage wxCpXmlMessage){
        String suiteId = wxCpXmlMessage.getAgentId();
        String authCorpId = "";

        OpenAuthDTO wxOpenAuthDTO = queryWxOpenAuthListByComponentIdAndAppId();
        if (null == wxOpenAuthDTO) {
            throw new BusinessException("无法获取开发平台参数信息");
        }
        MqMsgEventParam msgEventParam = new MqMsgEventParam();
        msgEventParam.setFromUserName(wxCpXmlMessage.getFromUserName());
        msgEventParam.setToUserName(wxCpXmlMessage.getToUserName());
        msgEventParam.setEvent(wxCpXmlMessage.getEvent());
        msgEventParam.setTenantId(wxOpenAuthDTO.getTenantId().toString());
        msgEventParam.setMsgType(wxCpXmlMessage.getMsgType());
        msgEventParam.setChangeType(wxCpXmlMessage.getChangeType());
        msgEventParam.setCreateTime(wxCpXmlMessage.getCreateTime());
        MsgEventFriendsParam friendsParam = new MsgEventFriendsParam();
        friendsParam.setTenantId(wxOpenAuthDTO.getTenantId().toString());
        friendsParam.setEmployeeCode(wxCpXmlMessage.getUserId());
        friendsParam.setFriendsCode(wxCpXmlMessage.getExternalUserId());
        msgEventParam.setParams(JsonUtils.toJsonStr(friendsParam));
        LogUtils.info(">>>>>>> DeleteFriendsFollow Param:{}", JsonUtils.toJsonStr(msgEventParam));
        mqServiceImpl.sendMsgEventMessage(msgEventParam);
    }
    //企业将客户分配给新的成员接替后，客户添加失败时回调该事件
    public void processFriendsTransferFailfMsgEvent(WxCpXmlMessage wxCpXmlMessage){
        String suiteId = wxCpXmlMessage.getAgentId();
        String authCorpId = "";

        OpenAuthDTO wxOpenAuthDTO = queryWxOpenAuthListByComponentIdAndAppId();
        if (null == wxOpenAuthDTO) {
            throw new BusinessException("无法获取开发平台参数信息");
        }
        MqMsgEventParam msgEventParam = new MqMsgEventParam();
        msgEventParam.setFromUserName(wxCpXmlMessage.getFromUserName());
        msgEventParam.setToUserName(wxCpXmlMessage.getToUserName());
        msgEventParam.setEvent(wxCpXmlMessage.getEvent());
        msgEventParam.setTenantId(wxOpenAuthDTO.getTenantId().toString());
        msgEventParam.setMsgType(wxCpXmlMessage.getMsgType());
        msgEventParam.setChangeType(wxCpXmlMessage.getChangeType());
        msgEventParam.setCreateTime(wxCpXmlMessage.getCreateTime());
        MsgEventFriendsParam friendsParam = new MsgEventFriendsParam();
        friendsParam.setTenantId(wxOpenAuthDTO.getTenantId().toString());
        friendsParam.setEmployeeCode(wxCpXmlMessage.getUserId());
        friendsParam.setFriendsCode(wxCpXmlMessage.getExternalUserId());
        friendsParam.setFailReason(wxCpXmlMessage.getFailReason());
        msgEventParam.setParams(JsonUtils.toJsonStr(friendsParam));
        LogUtils.info(">>>>>>> DeleteFriendsFollow Param:{}", JsonUtils.toJsonStr(msgEventParam));
        mqServiceImpl.sendMsgEventMessage(msgEventParam);
    }
    //有新增、修改、解散客户群时，回调该事件。收到该事件后，企业可以调用获取客户群详情接口获取客户群详情。
    public void processGroupChatMsgEvent(WxCpXmlMessage wxCpXmlMessage){
        String suiteId = wxCpXmlMessage.getAgentId();
        String authCorpId = "";

        OpenAuthDTO wxOpenAuthDTO = queryWxOpenAuthListByComponentIdAndAppId();
        if (null == wxOpenAuthDTO) {
            throw new BusinessException("无法获取开发平台参数信息");
        }
        MqMsgEventParam msgEventParam = new MqMsgEventParam();
        msgEventParam.setFromUserName(wxCpXmlMessage.getFromUserName());
        msgEventParam.setToUserName(wxCpXmlMessage.getToUserName());
        msgEventParam.setEvent(wxCpXmlMessage.getEvent());
        msgEventParam.setTenantId(wxOpenAuthDTO.getTenantId().toString());
        msgEventParam.setMsgType(wxCpXmlMessage.getMsgType());
        msgEventParam.setChangeType(wxCpXmlMessage.getChangeType());
        msgEventParam.setCreateTime(wxCpXmlMessage.getCreateTime());
        MsgEventFriendsParam friendsParam = new MsgEventFriendsParam();
        friendsParam.setTenantId(wxOpenAuthDTO.getTenantId().toString());
        friendsParam.setChatId(wxCpXmlMessage.getChatId());
        friendsParam.setUpdateDetail(wxCpXmlMessage.getUpdateDetail());
        friendsParam.setJoinScene(wxCpXmlMessage.getJoinScene());
        friendsParam.setQuitScene(wxCpXmlMessage.getQuitScene());
        friendsParam.setMemChangeCnt(wxCpXmlMessage.getMemChangeCnt());
        msgEventParam.setParams(JsonUtils.toJsonStr(friendsParam));
        LogUtils.info(">>>>>>> CREATE_PARTY:{}", JsonUtils.toJsonStr(msgEventParam));
        mqServiceImpl.sendMsgEventMessage(msgEventParam);
    }

    private OpenAuthDTO queryWxOpenAuthListByComponentIdAndAppId() {
        OpenAuthQuery query = new OpenAuthQuery();
        query.setConfigType(OpenApiConfigEnum.OPEN_CONFIG_TYPE_WECHAT_CP.getCode());
        query.setConfigSubType(WxCpAppTypeEnum.WECHAT_CP_APP_TYPE_SB.getCode());
        query.setAppType(WxCpServiceTypeConstant.CP_TYPE_SB);
        ResponseResult<OpenAuthDTO> responseResult = globalOpenFeignClient.queryOpenAuthOne(query);
        return responseResult.getData();
    }

}
